package com.wuxf.service;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuxf.entity.InvoiceInfo;
import com.wuxf.entity.TicketInfo;
import com.wuxf.entity.UserInfo;
import com.wuxf.entity.custom.InvoiceCustom;
import com.wuxf.entity.custom.TicketCustom;
import com.wuxf.mapper.custom.InvoiceMapper;
import com.wuxf.mapper.custom.TicketMapper;
import com.wuxf.util.DateUtils;

@Service
@SuppressWarnings({"rawtypes","unchecked"})
public class InvoiceService {
	@Autowired
	private InvoiceMapper invoiceMapper;
	@Autowired
	private TicketMapper ticketMapper;
	@Autowired
	private UserService userService;
	
	public PageInfo<InvoiceCustom> findByMap(PageInfo pageInfo,HashMap paraMap) {
		PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
		return new PageInfo<InvoiceCustom>(invoiceMapper.findByMap(paraMap));
	}
	
	public PageInfo<InvoiceCustom> findByMapAndCount(PageInfo pageInfo,HashMap paraMap) {
		PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
		return new PageInfo<InvoiceCustom>(invoiceMapper.findByMapAndCount(paraMap));
	}
	
	
	
	public PageInfo<InvoiceCustom> findByMapGroupByType(PageInfo pageInfo,HashMap paraMap) {
		PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
		return new PageInfo<InvoiceCustom>(invoiceMapper.findByMapGroupByType(paraMap));
	}
	
	public PageInfo<InvoiceCustom> findByMapGroupByArea(PageInfo pageInfo,HashMap paraMap) {
		PageHelper.startPage(pageInfo.getPageNum(), pageInfo.getPageSize());
		return new PageInfo<InvoiceCustom>(invoiceMapper.findByMapGroupByArea(paraMap));
	}
	
	
	public InvoiceCustom findByMap(String invoiceNum){
		HashMap paraMap = new HashMap();
		paraMap.put("invoiceNum", invoiceNum);
		List<InvoiceCustom> invoiceCustoms = invoiceMapper.findByMap(paraMap);
		if(invoiceCustoms!=null && invoiceCustoms.size()>0) {
			return invoiceCustoms.get(0);
		}
		return null;
	}
	
	public InvoiceCustom findById(String invoiceId){
		HashMap paraMap = new HashMap();
		paraMap.put("invoiceId", invoiceId);
		List<InvoiceCustom> invoiceCustoms = invoiceMapper.findByMap(paraMap);
		if(invoiceCustoms!=null && invoiceCustoms.size()>0) {
			return invoiceCustoms.get(0);
		}
		return null;
	}
	
	public List<InvoiceCustom> findByMap(HashMap paraMap){
		List<InvoiceCustom> invoiceCustoms = invoiceMapper.findByMap(paraMap);
		return invoiceCustoms;
	}
	
	public void saveInvoice(InvoiceInfo invoiceInfo) {
		invoiceInfo.setUpdateTime(DateUtils.getNowDate());
		invoiceInfo.setCreateTime(DateUtils.getNowDate());
		invoiceInfo.setAddDate(DateUtils.getNowDateByYYYYMMDD());
		
		UserInfo userInfo = userService.findByAreaId(invoiceInfo.getAreaId());
		
		HashMap paraMap = new HashMap();
		paraMap.put("ticketType", invoiceInfo.getInvoiceType());
		paraMap.put("userId", userInfo.getUserId());
		TicketCustom ticketCustom = ticketMapper.findByMap(paraMap).get(0);
		TicketInfo ticketInfo= new TicketInfo();
		BeanUtils.copyProperties(ticketCustom, ticketInfo);
		ticketInfo.setTicketNum(ticketInfo.getTicketNum()+invoiceInfo.getInvoiceNum());
		ticketMapper.updateTicket(ticketInfo);
		invoiceMapper.saveInvoice(invoiceInfo);
	}
	
	public void updateInvoice(String startNum,String endNum,InvoiceInfo invoiceInfo) {
		for (int i = Integer.parseInt(startNum); i <= Integer.parseInt(endNum); i++) {
			invoiceMapper.updateInvoiceByNum(invoiceInfo);
		}
	}
	
	public void updateInvoice(InvoiceInfo invoiceInfo) {
		HashMap temp = new HashMap();
		temp.put("invoiceId", invoiceInfo.getInvoiceId());
		List<InvoiceCustom> invoiceCustoms = invoiceMapper.findByMap(temp);
		
		//需要先修改对应区域的发票总数
		UserInfo userInfo = userService.findByAreaId(invoiceCustoms.get(0).getAreaId());
		HashMap paraMap = new HashMap();
		paraMap.put("userId", userInfo.getUserId());
		paraMap.put("ticketType", invoiceCustoms.get(0).getInvoiceType());
		paraMap.put("updateNum", invoiceInfo.getInvoiceNum()-invoiceCustoms.get(0).getInvoiceNum());
		ticketMapper.updateTicketByUserAndType(paraMap);
		invoiceMapper.updateInvoiceByNum(invoiceInfo);
	}
	
}
